package com.pengxun.mall.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import java.util.List;
import java.util.ArrayList;
import org.springframework.util.CollectionUtils;
import com.pengxun.mall.model.StoreGroup;
import com.pengxun.mall.dto.StoreGroupDTO;
import com.pengxun.mall.vo.StoreGroupVO;
import com.pengxun.mall.dao.StoreGroupDao;
import  com.pengxun.mall.dto.StoreGroupSearchDTO;
import com.pengxun.mall.service.StoreGroupService;
import org.springframework.beans.BeanUtils;
import com.szeastroc.common.utils.ValidationUtils;
import com.szeastroc.common.jdbc.crud.SqlBuilder;
import com.szeastroc.common.jdbc.page.Page;

@Service
public class StoreGroupServiceImpl implements StoreGroupService{

	@Resource
	private StoreGroupDao storeGroupDao;
	
	@Override
	public int insert(StoreGroupDTO storeGroupDTO) {
		ValidationUtils.validate(storeGroupDTO);
		StoreGroup storeGroup = new StoreGroup();
		BeanUtils.copyProperties(storeGroupDTO, storeGroup);
		return storeGroupDao.insert(storeGroup);
	}
	
    @Override
	public int batchInsert(List<StoreGroupDTO> storeGroupDTOs) {
	    ValidationUtils.validate(storeGroupDTOs);
    	if(CollectionUtils.isEmpty(storeGroupDTOs)) {
    		return 0;
    	}
    	List<StoreGroup> storeGroups = new ArrayList<StoreGroup>();
    	for(StoreGroupDTO storeGroupDTO:storeGroupDTOs) {
    		StoreGroup storeGroup = new StoreGroup();
    		BeanUtils.copyProperties(storeGroupDTO, storeGroup);
    		storeGroups.add(storeGroup);
    	}
		return storeGroupDao.batchInsert(storeGroups);
	}
	
	
    @Override
    public int update(StoreGroupDTO storeGroupDTO){
    	ValidationUtils.validate(storeGroupDTO);
		StoreGroup storeGroup = new StoreGroup();
		BeanUtils.copyProperties(storeGroupDTO, storeGroup);
        return storeGroupDao.updateBy(storeGroup,"id");
    }
   	
    @Override
    public int delete(Integer  id){
        if(id==null){
            return 0;
         }
    	StoreGroup storeGroup = new StoreGroup();
    	storeGroup.setId(id);
       return storeGroupDao.delete(storeGroup);
 	}
   
    @Override
    public List<StoreGroupVO> findAll(){
         List<StoreGroup> storeGroups = storeGroupDao.findAll(new SqlBuilder(StoreGroup.class).build());
    	 List<StoreGroupVO> storeGroupVOs = new ArrayList<StoreGroupVO>();
    	 for(StoreGroup storeGroup:storeGroups) {
    		 StoreGroupVO storeGroupVO = new StoreGroupVO();
    		 BeanUtils.copyProperties(storeGroup,storeGroupVO);
    		 storeGroupVOs.add(storeGroupVO);
    	 }
         return storeGroupVOs;
    }
    
    
    public Page findByPage(StoreGroupSearchDTO storeGroupSearchDTO){
             List<StoreGroup> storeGroups = storeGroupDao.find(storeGroupSearchDTO);
    	 List<StoreGroupVO> storeGroupVOs = new ArrayList<StoreGroupVO>();
    	 for(StoreGroup storeGroup:storeGroups) {
    		 StoreGroupVO storeGroupVO = new StoreGroupVO();
    		 BeanUtils.copyProperties(storeGroup,storeGroupVO);
    		 storeGroupVOs.add(storeGroupVO);
    	 }
    	 
    	 Page page = new Page();
    	 BeanUtils.copyProperties(storeGroupSearchDTO,page);
    	 page.setData(storeGroupVOs);
         return page;
    };
    
    public StoreGroupVO findById(Integer id){
          StoreGroup storeGroup = storeGroupDao.findFirst(new SqlBuilder(StoreGroup.class).eq("id",id).build());
          StoreGroupVO storeGroupVO = new StoreGroupVO();
    	  BeanUtils.copyProperties(storeGroup,storeGroupVO);
    	  
    	  return storeGroupVO;
    };

}
